using UnityEngine;

public class HUDItem
{
    public GameObject item;
    private RectTransform rect;

    private HUDItem()
    {
    }

    public static HUDItem Create(GameObject go)
    {
        HUDItem hudItem = new HUDItem();
      

        hudItem.item = Object.Instantiate(go);
        hudItem.item.transform.localScale = Vector3.one;
        hudItem.rect = hudItem.item.AddMissingComponent<RectTransform>();
        hudItem.rect.anchorMax=Vector2.zero;
        hudItem.rect.anchorMin=Vector2.zero;
        return hudItem;
    }

    public void Hide()
    {
        if (item)
            item.SetActive(false);
    }

    public void Show(Transform root)
    {
        item.SetParent(root);
        item.transform.localScale = Vector3.one;
        item.SetActive(true);
    }

    public void Check(Vector3 pos)
    {
        float scaleRate = 1.0F * 1080 / Screen.height;
        Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);
        rect.anchoredPosition = (screenPos * scaleRate);
        rect.localPosition = new Vector3(rect.anchoredPosition.x,rect.anchoredPosition.y,0);
    }
}